<?php 
include_once('includes.php'); 	

if(isset($_GET["id"]) && $_GET["id"]>0 )
{
	$id = (int) $_GET["id"];
	$sql = "select * from matchs where id=".addslashes($id);
	$match=db_fetch_one($sql);
	
	if(isset($_GET["task"]) && $_GET["task"] == "delete" )
	{
		db_execute("delete from channel where match_id=".$id); //delete all channel link
		db_delete("matchs",$id );
		Header("Location: /admin/match");
		exit;
	}
	$sql = "select * from channel where match_id=".addslashes($id);
	$channels=db_fetch_all($sql);
	
		
}

if($_SERVER["REQUEST_METHOD"]=="POST")
{
	$match_id = $_POST["id"];
	$data["id"] = $match_id;
	$data["competition_id"] = $_POST["competition_id"];
	$data["versus"] = $_POST["versus"];
	$data["game_week"] = $_POST["game_week"];
	
	$data["kick_off"] =  $_POST["kick_off"];
	$data["timezone"] = $_POST["timezone"];
	$data["is_feature"] = ($_POST["is_feature"]!=1)?0:1;
	$data["more_info"] =  $_POST["more_info"];
	if (get_magic_quotes_gpc()) {
		$data["more_info"] = stripslashes($_POST["more_info"]);
	}
	
	list($team1, $team2 ) = split('-', $data["versus"] );
	$data["team1_id"] = getTeamId( $team1 );
	$data["team2_id"] = getTeamId( $team2 );
	
	$now = convertTimeToLocal( date("Y-m-d H:i:s") , date("O")/100 );	
	$difference = strtotime(convertTimeToLocal($data["kick_off"],$data["timezone"])) - strtotime($now); 
	$minremain =  round($difference/60);
	
	if($minremain <= -150)
		$data["status"] = 2;	//end		
	else if($minremain <= 5)
		$data["status"] = 1;	//now playing	
	else
		$data["status"] = 0;
	
	if($data["id"] >0 )
		db_update("matchs",$data["id"],$data);
	else
		$match_id=db_insert("matchs",$data);
	
	//============================
	
	foreach($_POST["link"] as $i=> $link)
	{
		$data_channel["id"] = $_POST["channel_id"][$i];
			
		if($_POST["link"][$i] <>"" && $_POST["name"][$i]<>"") {
			$data_channel["match_id"] = $match_id;
			$data_channel["name"] = $_POST["name"][$i];
			$data_channel["link"] = $_POST["link"][$i];
			$data_channel["type"] = $_POST["type"][$i];
			$data_channel["vote"] = $_POST["vote"][$i];
			if($data_channel["id"] >0 )
				db_update("channel",$data_channel["id"],$data_channel);
			else
				$channel_id=db_insert("channel",$data_channel);			
		}
		else if($data_channel["id"] > 0 )
			db_delete("channel",$data_channel["id"]);
		
	}
	Header("Location: /admin/match");
	exit;
		
}

	
?>

<?php include_once('includes/header.php'); 	 ?>
<link type="text/css" href="/js/jquery-ui/themes/base/jquery.ui.all.css" rel="Stylesheet"/>
<link type="text/css" href="/js/jquery-ui/themes/base/jquery.ui.datetime.css" rel="Stylesheet"/>
<script type="text/javascript" src="/js/jquery-ui/js/jquery-ui-1.8.11.custom.min.js"></script>
<script type="text/javascript" src="/js/jquery-ui/js/jquery.ui.datetime.min.js"></script>

<script>
function addchannel(){
	var html = '<p> <label>Name</label> <input type="text" name="name[]" /> <label>Link</label> <input type="text" name="link[]" style="width:350px" /> <label>Type</label> <input type="text" name="type[]" style="width:100px" /> <label>Vote</label>	<input type="text" name="vote[]" style="width:50px" value="0"/>	</p>';
	$("#channel").append(html);
}
 $(function(){
    $('#kick_off').datetime({ value: '<?php echo $match->kick_off;?>' });
  });
</script>
<span class="title">Create New Match</span>


<form name="submitform" method="post" action="">
	<input type="hidden" name="id" value="<?php echo $match->id;?>"/>
	<p>
		<label for="is_feature">Feature match</label>
		<input type="checkbox" name="is_feature" id="is_feature" value="1" <?php echo ($match->is_feature)?"checked='checked'":"";?>/>
	</p>
	
	<p>
		<label>Versus</label>
		<input type="text" name="versus" value="<?php echo $match->versus;?>" style="width:293px"/>
	</p>
	
	<p>
		<label>Kick off</label>
		<input type="text" name="kick_off"  id="kick_off" />
			
		<select name="timezone" style="width:90px">
			<?php
			$tz = $match->timezone ;
			if(!$match)
				$tz = $_COOKIE["GMTClient"] ;
			for($i=-12; $i<=13; $i++)
			{
				$gmt = $i;
				if($i>0)
					$gmt = "+".$i;
				echo "<option value='".$i."'".(($i==$tz)?"selected='selected'":"").">GMT ".$gmt."</option>";
			}
			?>
			
		</select>
	</p>
	<p>
		<label>Competition</label>
		<select name="competition_id" style="width:200px">
				
		<?php 
		$sql = "select * from competition order by name asc ";
		$competitions=db_fetch_all($sql);
		foreach($competitions as $competition)
		{
			echo "<option value='".$competition->id."' ".(($competition->id==$match->competition_id)?'selected':'')." >".$competition->name."</option>";
		}
		?>	
		</select>	
		
	</p>
	<p>
		<label>Game week</label>
		<input type="text" name="game_week" value="<?php echo $match->game_week;?>"/>
	</p>
	<p>
		More info<br/>
		<textarea name="more_info" style="width:100%; height:100px"><?php echo $match->more_info;?></textarea>
	</p>
	
	<fieldset id="channel">
		<legend>Link channel</legend>
		<a href="javascript:;" onclick="addchannel()">+ Add more</a>
		
		<?php 
		if($channels)
		{
			foreach($channels as $channel)
			{
		?>
			<p>
				<input type="hidden" name="channel_id[]" value="<?php echo $channel->id;?>"/>
				<label>Name</label>
				<input type="text" name="name[]" value="<?php echo $channel->name;?>"/>
				<label>Link</label>
				<input type="text" name="link[]" style="width:350px" value="<?php echo $channel->link;?>"/>
				<label>Type</label>
				<input type="text" name="type[]" style="width:100px" value="<?php echo $channel->type;?>"/>
				<label>Vote</label>
				<input type="text" name="vote[]" style="width:50px"  value="<?php echo $channel->vote;?>"/>
			</p>
		<?php		
			
			}
		}
		else {
		?>
		
		<p>
			<label>Name</label>
			<input type="text" name="name[]" />
			<label>Link</label>
			<input type="text" name="link[]" style="width:350px" />
			<label>Type</label>
			<input type="text" name="type[]" style="width:100px" />
			<label>Vote</label>
			<input type="text" name="vote[]" style="width:50px" value="0"/>
		</p>
		<?php		
		}
		?>
		
	</fieldset>
	
	<div class="button"> <input type="submit" value="Save"/>   <input type="button" value="Cancel" onclick="history.go(-1);"/>   </div>
</form>

<?php include_once('includes/footer.php'); 	 ?>